/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.siglo21.ppo.persistence.dao;

import ar.com.siglo21.ppo.model.Socio;
import ar.com.siglo21.ppo.persistence.MySQLConnection;
import ar.com.siglo21.ppo.util.TrabajoIntegradorException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SocioDAOImpl implements SocioDAO {

    @Override
    public void agregar(Socio object) throws TrabajoIntegradorException, SQLException {
        String sql = "Insert into socio(nombre,apellido,dni,usuario,password) values ('" + object.getNombre()+  "','"+object.getApellido()+"',"+object.getDni()+",'"+object.getUsuario()+"','"+object.getPassword()+"')";
        MySQLConnection.getDbCon().insertORDeleteORUpdate(sql);
    }

    @Override
    public boolean existe(Socio object) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Socio leer(int id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void actualizar(Socio object) throws TrabajoIntegradorException, SQLException {
        String sql="UPDATE SOCIO SET nombre='"+object.getNombre()+"',"+ "apellido='"+object.getApellido()+"',"+"dni="+object.getDni()+"," + "usuario='"+object.getUsuario()+"',"+"password='"+object.getPassword()+"'" +" where id="+object.getId();
       MySQLConnection.getDbCon().insertORDeleteORUpdate(sql);
    }

    @Override
    public void eliminar(Socio object) throws SQLException, TrabajoIntegradorException {
        String sql = "DELETE FROM socio where id=" + object.getId();
        MySQLConnection.getDbCon().insertORDeleteORUpdate(sql);
    }
    
    @Override
    public List<Socio> listarSocios() throws SQLException, TrabajoIntegradorException {
        String sql = "Select * from socio";
        MySQLConnection.getDbCon().query(sql);
        Socio socio;

        List<Socio> listaSocios = new ArrayList();
        ResultSet resulset = MySQLConnection.getDbCon().query(sql);

        while (resulset.next()) {
            socio = new Socio();
            socio.setId(resulset.getInt(1));
            socio.setNombre(resulset.getString(2));
            socio.setApellido(resulset.getString(3));
            socio.setDni(resulset.getInt(4));
            socio.setUsuario(resulset.getString(5));
            socio.setPassword(resulset.getString(6));
            listaSocios.add(socio);
        }

        return listaSocios;

    }

}
